package com.crm.service;

import com.crm.base.BaseService;
import com.crm.dao.SaleChanceMapper;
import com.crm.enums.DevResult;
import com.crm.query.SaleChanceQuery;
import com.crm.utils.AssertUtil;
import com.crm.utils.PhoneUtil;
import com.crm.vo.SaleChance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.crm.enums.StateStatus;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class SaleChanceService extends BaseService<SaleChance, Integer> {

//    @Autowired
//    public SaleChanceMapper saleChanceMapper;

    public Map<String,Object> querySaleChanceByParams(SaleChanceQuery saleChanceQuery) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println("msg:" + saleChanceQuery);

        // 开启分页
        PageHelper.startPage(saleChanceQuery.getPage(), saleChanceQuery.getLimit());
        // 得到对应分页对象
        PageInfo<SaleChance> pageInfo = new PageInfo<SaleChance>(this.selectByParams(saleChanceQuery));

        // 设置map对象
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        // 设置分页好的列表
        map.put("data",pageInfo.getList());

        return map;
    }


    /**
     * 营销机会数据添加
     * @param saleChance
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void saveSaleChance(SaleChance saleChance) {
        /**
         * 1.参数校验
         * customerName:非空
         * linkMan:非空
         * linkPhone:非空 11位手机号
         * 2.设置相关参数默认值
         * state:默认未分配 如果选择分配人 state 为已分配
         * assignTime:如果 如果选择分配人 时间为当前系统时间
         * devResult:默认未开发 如果选择分配人devResult为开发中 0-未开发 1-开发中 2-开发成功 3-开发失败
         * isValid:默认有效数据(1-有效 0-无效)
         * createDate updateDate:默认当前系统时间
         * 3.执行添加 判断结果
         */

        //1、参数校验
        checkParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkPhone());
        //checkParams("百度","马小云","15700008929");
        //2、设置相关参数默认值
        saleChance.setState(StateStatus.UNSTATE.getType());
        saleChance.setDevResult(DevResult.UNDEV.getStatus());
        if(StringUtils.isNotBlank(saleChance.getAssignMan())){
            saleChance.setState((StateStatus.STATED.getType()));
            saleChance.setDevResult(DevResult.DEVING.getStatus());
            saleChance.setAssignTime(new Date());
        }
        saleChance.setIsValid(1);
        saleChance.setCreateDate(new Date());
        saleChance.setUpdateDate(new Date());
        AssertUtil.isTrue(insertSelective(saleChance)<1,"校验失败");
    }


    /**
     * 基本参数校验
     * @param customerName
     * @param linkMan
     * @param linkPhone
     */
    private void checkParams(String customerName, String linkMan, String linkPhone) {
        AssertUtil.isTrue(StringUtils.isBlank(customerName),"请输入客户名！");
        AssertUtil.isTrue(StringUtils.isBlank(linkMan),"请输入联系人姓名！");
        AssertUtil.isTrue(StringUtils.isBlank(linkPhone),"请输入手机号！");
        AssertUtil.isTrue(!(PhoneUtil.isMobile(linkPhone)),"手机号格式错误！");
    }


    /**
     * 参数校验
     * @param saleChance
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateSaleChances(SaleChance saleChance){
        /**
         * id:记录必须存在
         *  customerName:非空
         * linkMan:非空
         * linkPhone:非空，11位手机号
         * 2. 设置相关参数值
         * updateDate:系统当前时间
         * 原始记录 未分配 修改后改为已分配(由分配人决定)
         * state 0->1
         * assginTime 系统当前时间
         * devResult 0-->1
         * 原始记录 已分配 修改后 为未分配
         * state 1-->0
         * assignTime 待定 null
         * devResult 1-->0
         * 3.执行更新 判断结果
         */
          SaleChance temp= selectByPrimaryKey(saleChance.getId());
          AssertUtil.isTrue(null==temp,"待更新记录不存在！");
          checkParams(saleChance.getCustomerName(),saleChance.getLinkMan(),saleChance.getLinkMan());
          saleChance.setUpdateDate(new Date());
          if(StringUtils.isBlank(temp.getAssignMan())&&StringUtils.isNotBlank(saleChance.getAssignMan())){
            saleChance.setState(StateStatus.STATED.getType());
            saleChance.setAssignTime(new Date());
            saleChance.setDevResult(DevResult.DEVING.getStatus());
          }else if(StringUtils.isNoneBlank(temp.getAssignMan())&&StringUtils.isBlank(saleChance.getAssignMan())){
            saleChance.setState(StateStatus.UNSTATE.getType());
            saleChance.setAssignTime(null);
            saleChance.setDevResult(DevResult.UNDEV.getStatus());
            saleChance.setAssignMan("");
          }
          AssertUtil.isTrue(updateByPrimaryKeySelective(saleChance)<1,"机会数据更新失败！");
    }

    public void deleteSaleChance(Integer[] ids) {
        AssertUtil.isTrue(null==ids||ids.length==0,"请选择待删除记录！");
        AssertUtil.isTrue(deleteBatch(ids)!=ids.length,"记录删除失败！");
        //deleteBatch(ids);

    }
}
